package cn.haoma.base.geo.app;

/**
 * Created by ilioili on 2015/5/21.
 */
public class GeoEncoder {

    private static StringBuilder stringBuilder = new StringBuilder();

    public static String getGeoPath(double lantitude, double longtitude) {
        stringBuilder.setLength(0);
        double lantitudeStart = GeoConfig.LANTITUDE_START;
        double lantitudeEnd = GeoConfig.LANTITUDE_END;
        double longtitudeStart = GeoConfig.LONGTITUDE_START;
        double longtitudeEnd = GeoConfig.LONGTITUDE_END;

        for (int i = 0; i < GeoConfig.LEVEL; i++) {
            if (longtitude * 2 > longtitudeStart + longtitudeEnd) {
                if (lantitude * 2 > lantitudeEnd + lantitudeStart) {
                    stringBuilder.append(1);
                    longtitudeStart = (longtitudeStart + longtitudeEnd) / 2;
                    lantitudeEnd = (lantitudeStart + lantitudeEnd) / 2;
                } else {
                    stringBuilder.append(3);
                    longtitudeStart = (longtitudeStart + longtitudeEnd) / 2;
                    lantitudeStart = (lantitudeStart + lantitudeEnd) / 2;
                }
            } else {
                if (lantitude * 2 > lantitudeEnd + lantitudeStart) {
                    stringBuilder.append(0);
                    longtitudeEnd = (longtitudeStart + longtitudeEnd) / 2;
                    lantitudeEnd = (lantitudeStart + lantitudeEnd) / 2;
                } else {
                    stringBuilder.append(2);
                    longtitudeEnd = (longtitudeStart + longtitudeEnd) / 2;
                    lantitudeStart = (lantitudeStart + lantitudeEnd) / 2;
                }
            }
        }
        return stringBuilder.toString();
    }
}
